package com.atguigu.web.admin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.entity.LabelInfo;
import com.atguigu.model.enums.ItemType;
import com.atguigu.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelController {
    @Autowired
    private LabelInfoService service;

    @Operation(summary = "（根据类型）查询标签列表")//标签列表要根据type显示。type=1是公寓的标签列表
//    type=2是房间的标签列表
    @GetMapping("list")
    public Result<List<LabelInfo>> labelList(@RequestParam(required = false) ItemType type) {
        LambdaQueryWrapper<LabelInfo> queryWrapper = new LambdaQueryWrapper<>();//新建lambdaQueryWrapper条件查询对象
        queryWrapper.eq(type!=null,LabelInfo::getType,type);//把查询条件设计好，这个查询条件是在sql语句后
//        加上where type = 用户输入的type
        List<LabelInfo> list = service.list(queryWrapper);//将定义好的查询条件器塞入list方法返回的labelInfo list对象
        // 集合就是查询结果
        return Result.ok(list);//将查询返回的list交给前端
    }

   @Operation(summary = "保存或更新标签信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdateFacility(@RequestBody LabelInfo labelInfo){
        service.saveOrUpdate(labelInfo);
        return Result.ok();
   }

   @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("deleteById")
    public Result deletedLabelById(@RequestParam Long id) {
        service.removeById(id);
        return Result.ok();
   }
}
